package com.bc.web.controller.admin;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSJson;
import com.fs.common.util.FSJedis;
import com.bc.web.config.R;
import com.bc.web.model.Channel;
import com.bc.web.model.Dictionary;
import com.bc.web.model.FeeModel;
import com.bc.web.model.Merch;
import com.bc.web.model.RiskRule;
import com.bc.web.model.TradeStrategy;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class TradeStrategyController extends FSController {

	public void search() {
		Page<TradeStrategy> items = TradeStrategy.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		setAttr("payTypeList", Dictionary.dao.selectPayType());
		render("basedata/tradeStrategyList.html");
	}

	public void addPage() {
		List<RiskRule> riskRuleList = RiskRule.dao.selectAll();
		List<Dictionary> payTypeList = Dictionary.dao.selectByParentKey("payType");
		List<Channel> channelList = Channel.dao.selectAll();
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		setAttr("riskRuleList", riskRuleList);
		setAttr("payTypeList", payTypeList);
		setAttr("channelList", channelList);
		setAttr("feeModelList", feeModelList);
		render("basedata/tradeStrategyAdd.html");
	}

	public void updatePage() {
		TradeStrategy tradeStrategy = TradeStrategy.dao.findById(getPara("id"));
		Merch merch = Merch.dao.selectByMerchCode(tradeStrategy.getMerchCode());
		setAttr("tradeStrategy", tradeStrategy);
		List<RiskRule> riskRuleList = RiskRule.dao.selectAll();
		List<FeeModel> feeModelList = FeeModel.dao.selectAll();
		setAttr("riskRuleList", riskRuleList);
		setAttr("payType", Dictionary.dao.selectByKey(tradeStrategy.getPayTypeCode()));
		setAttr("channel", Channel.dao.selectByCode(tradeStrategy.getChannelCode()));
		setAttr("feeModelList", feeModelList);
		setAttr("merch", merch);
		render("basedata/tradeStrategyUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
		String merchCode = getPara("merchCode");
		String channelCode = getPara("channelCode");
		String[] payTypeList = getParaValues("payType");
		String[] merchFeeIdList = getParaValues("merchFeeId");
		String[] riskRuleIdList = getParaValues("riskRuleId");
		String[] channelSubMerchCodeList = getParaValues("channelSubMerchCode");
		
		Set<String> tmpSet = new HashSet<>();
		
		for (String payType : payTypeList) {
			tmpSet.add(payType);
		}
		
		if(tmpSet.size() < payTypeList.length){
			renderFail("支付类别不允许重复.");
		}else{
			for (int i = 0; i < payTypeList.length; i++) {
				TradeStrategy ts = TradeStrategy.dao.selectByMerchCodePayType(merchCode, payTypeList[i]);
				if(ts != null){
					ts.setMerchFeeId(Integer.valueOf(merchFeeIdList[i]));
					ts.setRiskRuleId(Integer.valueOf(riskRuleIdList[i]));
					ts.update();
				}else{
					ts = new TradeStrategy();
					ts.setMerchCode(merchCode);
					ts.setChannelCode(channelCode);
					ts.setPayTypeCode(payTypeList[i]);
					ts.setMerchFeeId(Integer.valueOf(merchFeeIdList[i]));
					ts.setRiskRuleId(Integer.valueOf(riskRuleIdList[i]));
					ts.setChannelSubMerchCode(channelSubMerchCodeList[i]);
					ts.save();
				}
				if (R.IS_REDIS) {
					FSJedis.update(R.RK_TS, ts.getMerchCode() + ts.getPayTypeCode(), FSJson.toString(ts));
				}
			}
			renderSuccess();
		}
	}

	@Before(Tx.class)
	public void update() {
		TradeStrategy tradeStrategy = getModel(TradeStrategy.class, true);
		tradeStrategy.setUpdateTime(FSDate.getDateTime());
		tradeStrategy.update();
		if (R.IS_REDIS) {
			FSJedis.update(R.RK_TS, tradeStrategy.getMerchCode() + tradeStrategy.getPayTypeCode(), FSJson.toString(tradeStrategy));
		}
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		TradeStrategy t = TradeStrategy.dao.findById(getPara("id"));
		TradeStrategy.dao.deleteById(t.getId());
		if (R.IS_REDIS) {
			FSJedis.remove(R.RK_TS, t.getMerchCode() + t.getPayTypeCode());
		}
		renderSuccess(false);
	}
	
}
